package com.boot.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Package com.boot.proxy
 */
public class ProxyHandler implements InvocationHandler {

    private Object object;
    public ProxyHandler(Object object){
        this.object = object;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Before invoke "  + method.getName());
        Object result = method.invoke(object, args);
        System.out.println("After invoke " + method.getName());
        return result;
    }

    public static void main(String[] args) {
        UserManagerImpl userManager = new UserManagerImpl();
        InvocationHandler handler = new ProxyHandler(userManager);
        UserManager proxyHello = (UserManager) Proxy.newProxyInstance(userManager.getClass().getClassLoader(), userManager.getClass().getInterfaces(), handler);
        proxyHello.addUser("zhangsw","123");
    }
}
